/**
 * 
 */
 
 
package com.simpleauth.svc;

import java.io.IOException;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;


public class SimpleAuthSvcClient {
	String service_url;
	
	public SimpleAuthSvcClient (String service_url) {
		this.setServiceUrl(service_url);
	}
	
	private String getServiceUrl() {
		return this.service_url;
	}
	
	private void setServiceUrl(String service_url) {
		this.service_url = service_url;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		boolean response = false;
		Vector roles = new Vector();
		String authMethod = new String();
        if (args.length < 3) {
        	System.out.println("Usage: java SimpleAuthSvcClient [(authenticate username password [resource] | authorize servicename username rolename [resource] | getRolesForUser servicename username [resource])]");
        	System.exit(-1);
        }
        SimpleAuthSvcClient client = new SimpleAuthSvcClient("http://localhost:11010/RPC2");
        
        try {
        	authMethod = args[0];
            if (authMethod.equalsIgnoreCase("authenticate")) {
            	String username = args[1];
            	String password = args[2];
            	String resource;
            	try {
            		resource = args[3];
            	} catch (ArrayIndexOutOfBoundsException e) {
            		resource = new String("");
            	}
            	response = client._run_authenticate(username, password, resource);
            	System.out.println("Response for " + authMethod + ": " + response + ".");
            } else if (authMethod.equalsIgnoreCase("authorize")) {
            	String servicename = args[1];
            	String username = args[2];
            	String rolename = args[3];
            	String resource;
            	try {
            		resource = args[4];
            	} catch (ArrayIndexOutOfBoundsException e) {
            		resource = new String("");
            	}
            	response = client._run_authorize(servicename, username, rolename, resource);
            	System.out.println("Response for " + authMethod + ": " + response + ".");
            } else if (authMethod.equalsIgnoreCase("getRolesForUser")) {
            	String servicename = args[1];
            	String username = args[2];
            	String resource;
            	try {
            		resource = args[3];
            	} catch (ArrayIndexOutOfBoundsException e) {
            		resource = new String("");
            	}
            	roles = client._run_getRolesForUser(servicename, username, resource);
            	System.out.println("Response for " + authMethod + ": " + roles.toString() + ".");
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        	System.out.println("To use authenticate, you must pass in username and password!");
        }
	}
	
	private boolean _run_authenticate(String username, String password, String resource) {
		boolean authenticated = false;
		try {
			Vector crypt = new Vector();
			crypt.addElement("");
			crypt.addElement("");
			String authoverride = new String("");
		    authenticated = this.authenticate(username, password, crypt, authoverride, resource);
		} catch (IOException e) {
			System.out.println("IO Error: " + e);
			e.printStackTrace();
		} catch (XmlRpcException e) {
			System.out.println("XmlRpc Error: " + e);
			e.printStackTrace();
		}
		return authenticated;
    }
	
	private boolean _run_authorize(String servicename, String username, String rolename, String resource) {
		boolean authorized = false;
		String authoverride = new String("");
		try {
			authorized = this.authorize(servicename, username, rolename, authoverride, resource);
		} catch (IOException e) {
			System.out.println("IO Error: " + e);
			e.printStackTrace();
		} catch (XmlRpcException e) {
			System.out.println("XmlRpc Error: " + e);
			e.printStackTrace();
		}
		return authorized;
    }
	
	private Vector _run_getRolesForUser(String servicename, String username, String resource) {
		Vector results = new Vector();
		String authoverride = new String("");
		try {
			results = this.getRolesForUser(servicename, username, authoverride, resource);
		} catch (IOException e) {
			System.out.println("IO Error: " + e);
			e.printStackTrace();
		} catch (XmlRpcException e) {
			System.out.println("XmlRpc Error: " + e);
			e.printStackTrace();
		}
		return results;
	}
	
	/**
	 * 
	 * @param username
	 * @param password
	 * @param crypt
	 * @param authoverride
	 * @param resource
	 * @return
	 * @throws IOException
	 * @throws XmlRpcException
	 */

	public boolean authenticate(String username, String password,
			                    Vector crypt, String authoverride, 
			                    String resource)
	    throws IOException, XmlRpcException {
		// starts assuming false
		boolean authenticated = false;
		
        //create client
		XmlRpcClient client = new XmlRpcClient( this.getServiceUrl() );
		
		//add parameters
        Vector params = new Vector();
        params.addElement(new String(username));
        params.addElement(new String(password));
        params.addElement(crypt);
        params.addElement(new String(authoverride));
        params.addElement(new String(resource));
        
        // run remote
        Object result = client.execute("authenticate", params);
        String resultString = result.toString();
        
        if (resultString.equalsIgnoreCase("true")) {
        	authenticated = true;
        } else {
        	authenticated = false;
        }
	    return authenticated;		

	}
	
	public boolean authenticate(String username, String password,
			                    Vector crypt, String authoverride)
	    throws IOException, XmlRpcException {
        String resource = new String("");
        
        return authenticate(username, password, crypt, authoverride, resource);
	}
	
	/**
	 * just overloading authenticate to accept a crypt list and a auth_override name
	 */
	public boolean authenticate(String username, String password)
	    throws IOException, XmlRpcException {
		Vector crypt = new Vector();
		crypt.addElement("");
		crypt.addElement("");
		String authoverride = new String("");
		String resource = new String("");
		return authenticate(username, password, crypt, authoverride, resource);
	}
	
	
	/**
	 * 
	 * @param servicename
	 * @param username
	 * @param rolename
	 * @param authoverride
	 * @param resource
	 * @return
	 * @throws IOException
	 * @throws XmlRpcException
	 */
	
	public boolean authorize(String servicename, String username, 
			                 String rolename, String authoverride, 
			                 String resource) 
	    throws IOException, XmlRpcException {
		boolean authorized = false;
        
		//create client
		XmlRpcClient client = new XmlRpcClient( this.getServiceUrl() );
		
		//add parameters
        Vector params = new Vector();
        params.addElement(new String(servicename));
        params.addElement(new String(username));
        params.addElement(new String(rolename));
        params.addElement(new String(authoverride));
        params.addElement(new String(resource));
        
        // run remote
        Object result = client.execute("authorize", params);
        String resultString = result.toString();
        
        if (resultString.equalsIgnoreCase("true")) {
        	authorized = true;
        } else {
        	authorized = false;
        }		
		return authorized;
	}
	
	public boolean authorize(String servicename, String username, String rolename) 
	    throws IOException, XmlRpcException {
		String authoverride = new String("");
		String resource = new String("");
		return authorize(servicename, username, rolename, authoverride, resource);
	}
	
	public Vector getRolesForUser(String servicename, String username, String authoverride, String resource) 
	    throws IOException, XmlRpcException {
		Vector results = new Vector();
		//create client
		XmlRpcClient client = new XmlRpcClient( this.getServiceUrl() );
		
		//add parameters
        Vector params = new Vector();
        params.addElement(new String(servicename));
        params.addElement(new String(username));
        params.addElement(new String(authoverride));
        params.addElement(new String(resource));
        
        // run remote
        Object untypedresult = client.execute("get_roles_for_user", params);
        results = (Vector) untypedresult;

		return results;
	}
	
	public Vector getRolesForUser(String servicename, String username) 
	    throws IOException, XmlRpcException {
		String authoverride = new String("");
		String resource = new String("");
		return getRolesForUser(servicename, username, authoverride, resource);
	}
}
